﻿using System;
using System.Collections.Generic;
using CosyTrade.Store;
using CosyTrade.UI;
using DevExpress.XtraTreeList.Nodes;
using CosyTrade.ClientForm.Properties;

namespace CosyTrade.ClientForm.Store
{
    public partial class ProductCategoryForm : FormBase
    {
        private ProductCategory currentObject;

        public ProductCategoryForm()
        {
            InitializeComponent();
        }

        protected override void InitCommandBar()
        {
            ToolbarItems.AddCommandItem(new ToolbarCommandItem("NewCate", Resources.add, "新建商品分类", true, true, true, delegate { AddNewProductCategory(); }));
            ToolbarItems.AddCommandItem(new ToolbarCommandItem("DelCate", Resources.delete, "删除商品分类", true, true, true, delegate { DeleteProductCategory(); }));
            ToolbarItems.AddCommandItem(new ToolbarCommandItem("Refresh", Resources.arrow_refresh, "刷新列表", true, true, true, delegate { LoadCategoryTree(); }));
            ToolbarItems.AddCommandItem(new ToolbarCommandItem("EditCate", Resources.table, "商品分类属性", true, true, true, delegate { UpdateProductCategory(); }));
            ToolbarItems.AddCommandItem(new ToolbarCommandItem("CloseForm", Resources.door_out, "关闭&C", true, true, true, delegate { Close(); }));
        }

        private void ProductCategoryForm_Load(object sender, EventArgs e)
        {

            LoadCategoryTree();
        }

        private void LoadCategoryTree()
        {
            productCategoryTree.ClearNodes();

            IList<ProductCategory> productCategories = Operation.ProductManager.GetProductRootList();

            if (productCategories == null) return;
            foreach (ProductCategory category in productCategories)
            {
                TreeListNode treeListNode = productCategoryTree.AppendNode(new object[] {category.CategoryChinese,category.CategorySpanish,category},null);
                treeListNode.HasChildren = true;
                treeListNode.Tag = false;
            }
        }

        #region 方法

        private void AddNewProductCategory()
        {

            ProductCategoryDetail frmProperty = new ProductCategoryDetail();
            frmProperty.IsNew = true;
            frmProperty.ProductCategorys = new ProductCategory();
            if (frmProperty.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;

            ProductCategory productCategory = frmProperty.ProductCategorys.GetChanges() as ProductCategory;

            if (productCategory == null) return;
            productCategory.IsRootCategory = productCategory.ParentCategoryBase == null;

            Operation.ProductManager.CreateProductCategory(productCategory);

            LoadCategoryTree();
        }

        private void UpdateProductCategory()
        {
            if (currentObject == null) return;

            ProductCategoryDetail frmProperty = new ProductCategoryDetail();
            frmProperty.ProductCategorys = currentObject;
            frmProperty.IsNew = false ;
            if (frmProperty.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;

            ProductCategory productCategory = frmProperty.ProductCategorys.GetChanges() as ProductCategory;
            if (productCategory == null) return;
            productCategory.IsRootCategory = productCategory.ParentCategoryBase == null;
            Operation.ProductManager.UpdateProductCategory(productCategory);
            LoadCategoryTree();
        }

        private void DeleteProductCategory()
        {
            if (currentObject == null) return;
            IList<Product> products = Operation.ProductManager.GetProductList(currentObject.PrimaryKey);
            IList<ProductCategory> productCategorys = Operation.ProductManager.GetProductSubList(currentObject.PrimaryKey);
            if ((products != null && products.Count > 0) || productCategorys != null && productCategorys.Count>0)
            {
                Functions.ShowMessageBox("无法执行删除操作！'" + currentObject.CategoryChinese + "(" + currentObject.CategorySpanish + ")'分类正在被使用。");
                return;
            }
            if (Functions.ShowConfigBox("确定要删除'" + currentObject.CategoryChinese + "(" + currentObject.CategorySpanish + "') 分类吗?") != System.Windows.Forms.DialogResult.Yes)
                return;
            Operation.ProductManager.DeleteProductCategory(currentObject.PrimaryKey);
            LoadCategoryTree();
        }

        #endregion

        #region 事件

        private void productCategoryTree_BeforeExpand(object sender, DevExpress.XtraTreeList.BeforeExpandEventArgs e)
        {
            if (e.Node.Tag == null) return;
            if ((bool)e.Node.Tag) return;
            e.Node.Tag = true;

            ProductCategoryBase currentCategory = (ProductCategoryBase)e.Node.GetValue("pME");
            if (currentCategory == null) return;
            IList<ProductCategory> productCategory = Operation.ProductManager.GetProductSubList(currentCategory.PrimaryKey);
            if (productCategory == null) return;

            foreach (ProductCategory category in productCategory)
            {
                TreeListNode listNode =
                    productCategoryTree.AppendNode(
                        new object[] {category.CategoryChinese, category.CategorySpanish, category},
                        e.Node);
                listNode.HasChildren = true;
                listNode.Tag = false;
            }
        }

        private void productCategoryTree_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Clicks == 1 && e.Button == System.Windows.Forms.MouseButtons.Right)
                popupMenu1.ShowPopup(productCategoryTree.PointToScreen(e.Location));
        }

        private void productCategoryTree_Click(object sender, EventArgs e)
        {
            if (productCategoryTree.Selection.Count > 0)
                currentObject = (ProductCategory) productCategoryTree.Selection[0].GetValue("pME");
            barProperty.Enabled = barDelete.Enabled = currentObject != null;
        }

        #endregion

        #region 菜单事件

        private void barNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddNewProductCategory();
        }

        private void barDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DeleteProductCategory();
        }

        private void barRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadCategoryTree();
        }

        private void barProperty_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UpdateProductCategory();
        }

        #endregion
    }
}
